//
//  SpriteFrog.m
//  FrogDrying
//
//  Created by Le Thanh Hai on 4/6/13.
//  Copyright 2013 __MyCompanyName__. All rights reserved.
//

#import "SpriteFrog.h"

@interface SpriteFrog()
-(void)update;
@end

@implementation SpriteFrog
@synthesize state, currentLotus, allLotus, completeEvent, colour;

-(id)init
{
    if (self = [super init]) {
        CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
        NSMutableArray *frogs = [[[NSMutableArray alloc] init] autorelease];
        colour = arc4random() % 6 + 1;
        
        for (int i = 1; i <= 6; i++) {
            CCSpriteFrame* frame = [cache spriteFrameByName:[NSString stringWithFormat:@"frog_%d_0%d.png", colour, i]];
            [frogs addObject:frame];
        }
        frogNormal = [[CCAnimation alloc] initWithSpriteFrames:frogs delay:0.5/5];
        
        float rand = arc4random() % 5 + 5;
        rand /= 10;
        [self schedule:@selector(update) interval:rand];
        
        NSMutableArray *waves = [[[NSMutableArray alloc] init] autorelease];
        for (int i = 1; i <= 5; i++) {
            CCSpriteFrame* frame = [cache spriteFrameByName:[NSString stringWithFormat:@"wavedrop_0%d.png", i]];
            [waves addObject:frame];
        }
        waveDrop = [[CCAnimation alloc] initWithSpriteFrames:waves delay:1.0/5.0];
        state = kStateNormal;
    }
    return self;
}

-(void)setState:(StateFrog)state_
{
    if(state == state_) return;
	state = state_;    
    switch (state_) {
        case kStateNormal:
            [self stopAllActions];
            [self setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"frog_%d_02.png", colour]]];
            break;
            
        case kStateJump:
            [self stopAllActions];
            [self runAction:[CCAnimate actionWithAnimation:frogNormal]];
            break;
            
        case kStateDrag:
            [self stopAllActions];
            [self setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"frog_%d_02.png", colour]]];
            self.currentLotus.hasFrog = NO;
            break;
    
        case kStateFall:
            [self stopAllActions];
            [self runAction:[CCSequence actions:[CCMoveBy actionWithDuration:0.4 position:ccp(0, -100)], [CCCallBlock actionWithBlock:^{
                [self runAction:[CCSequence actions:[CCAnimate actionWithAnimation:waveDrop], [CCCallBlock actionWithBlock:^{
                    self.currentLotus = [allLotus objectAtIndex:0];
                    state = kStateNormal;
                }], nil]];
            }], nil]];
            break;
            
        case kStateReset:
            [self resetFrog];
            self.currentLotus.hasFrog = NO;
            self.currentLotus = [allLotus objectAtIndex:0];
            state = kStateNormal;
            
        default:
            break;
    }
}

-(void)resetFrog
{
    [self unscheduleAllSelectors];
    CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
    NSMutableArray *frogs = [[[NSMutableArray alloc] init] autorelease];
    colour = arc4random() % 6 + 1;
    
    for (int i = 1; i <= 6; ++i) {
        CCSpriteFrame* frame = [cache spriteFrameByName:[NSString stringWithFormat:@"frog_%d_0%d.png", colour, i]];
        [frogs addObject:frame];
    }
    
    frogNormal = [[CCAnimation alloc] initWithSpriteFrames:frogs delay:0.5/5];
    
    float rand = arc4random() % 5 + 5;
    rand /= 10;
    [self schedule:@selector(update) interval:rand];
}

-(void)jump:(CGPoint)position completeEvent:(CompleteEvent)handler
{
    self.completeEvent = handler;
    self.state = kStateJump;
    float height = arc4random() % 20 + 50;
    CCJumpTo *jump = [CCJumpTo actionWithDuration:0.5 position:ccpAdd(position, ccp(0, 35)) height:height jumps:1];
    [self runAction:[CCSequence actionOne:jump two:[CCCallBlock actionWithBlock:^{
        self.state = kStateNormal;
        if (self.completeEvent) {
            self.completeEvent();
        }
    }]]];
}

-(void)setCurrentLotus:(LotusAnimation *)currentLotus_
{
    currentLotus = currentLotus_;
    currentLotus.hasFrog = YES;
    self.position = ccpAdd(currentLotus.position, ccp(0, 35));
}

-(void)update
{
    if (self.state == kStateJump || self.state == kStateDrag || self.state == kStateFall) return;
    int index = [allLotus indexOfObject:currentLotus];
    if (index < [allLotus count] - 1) {
        nextLotus = [allLotus objectAtIndex:index + 1];
        if (!nextLotus.hasFrog) {
            currentLotus.hasFrog = NO;
            nextLotus.hasFrog = YES;
            [self jump:nextLotus.position completeEvent:^{
                currentLotus.waveActive = YES;
            }];
            currentLotus = nextLotus;
        }
    } else {
        self.currentLotus.hasFrog = NO;
        self.currentLotus = [allLotus objectAtIndex:0];
        [self resetFrog];
    }
}

-(CGRect)rect {
    CGPoint point = self.position;
    float w = self.contentSize.width;
    float h = self.contentSize.height;
    CGRect rect = CGRectMake(point.x - w/2, point.y - h/2, w, h);
    return rect;
}
@end
